package com.xlh.dao.conclude.helper;

import cn.hutool.core.collection.CollectionUtil;
import com.xlh.dao.conclude.ConcludeMapper;
import com.xlh.pojo.conclude.Conclude;
import com.xlh.pojo.conclude.example.ConcludeExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author cfz
 */
@Repository
public class ConcludeMapperHelper {

    @Autowired
    private ConcludeMapper concludeMapper;


    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public Conclude get(Long id) {

        ConcludeExample concludeExample = new ConcludeExample();
        concludeExample.createCriteria().andIdEqualTo(id).andIsDeleteEqualTo(false);
        List<Conclude> concludes = concludeMapper.selectByExample(concludeExample);
        if(CollectionUtil.isNotEmpty(concludes)){
            return concludes.get(0);
        }
        return null;
    }

    /**
     * 根据课程ID查询
     * @param courseId
     * @return
     */
    public List<Conclude> findByCondition(Long courseId) {

        ConcludeExample concludeExample = new ConcludeExample();
        concludeExample.createCriteria().andCourseIdEqualTo(courseId).andIsDeleteEqualTo(false);
        List<Conclude> concludes = concludeMapper.selectByExample(concludeExample);
        return concludes;
    }

    /**
     * 查询根据条件
     * @param courseId 课程ID
     * @param name  名称
     * @return
     */
    public List<Conclude> findByCondition(Long courseId, String name) {

        ConcludeExample concludeExample = new ConcludeExample();
        concludeExample.createCriteria()
                .andCourseIdEqualTo(courseId)
                .andNameEqualTo(name)
                .andIsDeleteEqualTo(false);
        List<Conclude> concludes = concludeMapper.selectByExample(concludeExample);
        return concludes;
    }


    /**
     * <p>
     *     逻辑上只有一个课程目标可以公开
     * </p>
     * @param courseId
     * @return
     */
    public Conclude getConcludeIsPublic(Long courseId) {

        ConcludeExample concludeExample = new ConcludeExample();
        concludeExample.createCriteria().andCourseIdEqualTo(courseId).andIsDeleteEqualTo(false).andIsPublishEqualTo(true);
        List<Conclude> concludes = concludeMapper.selectByExample(concludeExample);
        if(CollectionUtil.isNotEmpty(concludes)){

            return concludes.get(0);
        }
        return null;
    }



    /**
     * 插入结果
     * @param conclude
     * @return true 插入成功 false 插入失败
     */
    public Boolean insert(Conclude conclude){

        Integer insertRecord = concludeMapper.insertSelective(conclude);

        if(insertRecord == 0){
            return false;
        }
        return true;
    }

    /**
     * 根据达成度ID删除
     * @param id
     */
    public void delete(Long id) {

        Conclude record = new Conclude();
        record.setIsDelete(true);
        ConcludeExample example = new ConcludeExample();
        example.createCriteria().andIdEqualTo(id);
        concludeMapper.updateByExampleSelective(record, example);
    }


    /**
     * 执行更新操作
     * @param conclude
     */
    public void update(Conclude conclude) {

        ConcludeExample example = new ConcludeExample();
        example.createCriteria().andIdEqualTo(conclude.getId()).andIsDeleteEqualTo(false);
        concludeMapper.updateByExampleSelective(conclude, example);

    }

    public void updateByCourseId(Conclude conclude){

        ConcludeExample example = new ConcludeExample();
        example.createCriteria().andCourseIdEqualTo(conclude.getCourseId()).andIsDeleteEqualTo(false);
        concludeMapper.updateByExampleSelective(conclude, example);
    }




}
